package com.food.msgupdownbackend.adapter.inbound;

import com.food.msgupdownbackend.application.ImgApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Arrays;

/**
 * 上传下载图片
 * @author 24270
 */
@RestController
@RequestMapping("/img")
@CrossOrigin("*")
public class ImgController {
    private ImgApplicationService service;

    @Autowired
    public ImgController(ImgApplicationService service) {
        this.service = service;
    }

    /**
     *
     * @param file
     * @param request
     * @return String图片路径
     * @throws Exception
     */
    @PostMapping(path = "/upload/{id}")
    public String upload(@RequestParam("photos") MultipartFile file, HttpServletRequest request, @PathVariable String id) throws Exception {
        return service.upload(file, request, id);
    }

    /**
     *
     * @param imgName
     * @param request
     * @param response
     * @return 图片
     */
    @GetMapping(path = "/download")
    public String download(@RequestParam("imgName") String imgName, HttpServletRequest request, HttpServletResponse response) {
        return service.download(imgName, request, response);
    }
}
